<template>
  <div class="qrcode-wrapper">
    <centerHeader/>

    <div class="qrcode-wrap">
      <div class="top-wrap">
        <div class="title_box">
          <span class="title-01">我的返现</span>
          <i class="el-icon-arrow-down"></i>
        </div>
      </div>
      <div class="commission">
        <div class="clearfix com_box">
          <span class="fl">
            当前返现
            <span class="com_money">{{user_info.user_fanli}}</span>元
          </span>
          <div class="fr">
            <el-button
              type="danger"
              size="mini"
              round
              style="background-color:#bc9f5b;color:#ffffff;font-size:0.3rem"
              @click="open"
            >申请提现</el-button>
          </div>
        </div>
        <transition name="slide-fade" class="tran_box">
          <div class="hotKey-wrap" v-if="isDetail">
            <div class="shop_detail">
              <i class="el-icon-close shop_close" @click="close"></i>
              <div class="add_clerk">
                <el-form label-width="80px" :model="formLabel">
                  <el-form-item label="提现金额">
                    <el-input type="text" v-model="formLabel.tixian_money" clearable></el-input>
                  </el-form-item>
                  <el-form-item label="姓名">
                    <el-input type="text" v-model="formLabel.tixian_username" clearable></el-input>
                  </el-form-item>
                  <el-form-item label="开户行">
                    <el-input type="text" v-model="formLabel.tixian_bankname" clearable></el-input>
                  </el-form-item>
                  <el-form-item label="银行卡号">
                    <el-input type="text" v-model="formLabel.tixian_banknumber" clearable></el-input>
                  </el-form-item>
                </el-form>
                <div class="submit-box">
                  <button class="submit-btn" @click="submit">提交申请</button>
                </div>
              </div>
            </div>
          </div>
        </transition>
        <div class="com_list">
          <div class="com_title1">返现收入明细</div>
          <ul class="com_ul" v-for="item in comm_list" :key="item.order_id">
            <li>
              <div class="clearfix com_top">
                <div class="com_name">{{item.store_name}}</div>
                <div class="com_price" v-if="user_type == 'store'">+{{item.total_store_fanli}}</div>
                <div
                  class="com_price"
                  v-if="user_info.user_type == 3"
                >+{{item.store_user_commission}}</div>
              </div>
              <div class="clearfix">
                <div class="com_orderId">订单号：{{item.order_sn}}</div>
                <div class="com_time">{{item.create_time | formatDate}}</div>
              </div>
            </li>
          </ul>
        </div>
      </div>
      <div class="com_title" @click="query_fanli">
        <i class="el-icon-plus"></i>
        加载更多
      </div>
    </div>

    <centerFooter/>
  </div>
</template>

<script type="text/ecmascript-6">
import centerHeader from "@/views/center/components/centerHeader";
import centerFooter from "@/views/center/components/centerFooter";
import constant from "@/utils/constant.js";
import { formatTime } from "@/utils/index.js";
// 引用API文件
import http from "@/utils/http.js";

export default {
  name: "fanli",
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入提现金额"));
      } else {
        if (this.ruleForm2.checkPass !== "") {
          this.$refs.ruleForm2.validateField("checkPass");
        }
        let reg = /^[0-9]{1,7}\.([0-9]{2})$/;
        if (!reg.test(value) && value > this.user_info.total_commission) {
          callback(new Error("请输入正确的提现金额"));
        }
        callback();
      }
    };
    return {
      user_info: {
        total_commission: 0
      },
      p: 0,
      comm_list: [],
      isDetail: false,
      formLabel: {
        tixian_money: "",
        tixian_username: "",
        tixian_bankname: "",
        tixian_banknumber: ""
      },
      user_type: "",
      rules2: {
        tixian_money: [{ validator: validatePass, trigger: "blur" }]
      }
    };
  },
  components: {
    centerHeader,
    centerFooter
  },
  filters: {
    formatDate(time) {
      return formatTime(time, "{y}-{m}-{d}");
    }
  },
  methods: {
    userinfo() {
      http.get(this, {
        url: "ucenter/user_info",
        data: {
          token: localStorage.getItem("user_token")
        },
        dataType: "json",
        success: function(data) {
          console.log("用户信息:", data);
          if (data.status == 1) {
            this.user_info = data.info;
            this.user_info.user_fanli = this.user_info.total_commission;
            this.p = 0;
            this.comm_list = [];
            this.query_fanli();
          }
        }
      });
    },
    close() {
      this.isDetail = false;
    },
    open() {
      this.isDetail = true;
    },
    submit() {
      if (
        this.formLabel.tixian_money == "" ||
        this.formLabel.tixian_username == "" ||
        this.formLabel.tixian_bankname == "" ||
        this.formLabel.tixian_banknumber == ""
      ) {
        return false;
      }
      let user_type = localStorage.getItem("user_type");
      let tixian_from = "store";
      if (user_type == 3) {
        tixian_from = "store";
      } else {
        tixian_from = "pathform";
      }

      if (this.formLabel.tixian_money > this.total_commission) {
        this.$message({
          message: "提现金额不能大于佣金",
          type: "warning"
        });
        return false;
      }
      http.post(this, {
        url: "tixian/add",
        data: {
          token: localStorage.getItem("user_token"),
          tixian_from: tixian_from,
          ...this.formLabel
        },
        dataType: "json",
        success: function(data) {
          console.log("用户信息:", data);
          if (data.status == 1) {
            this.$message.success("申请提现完成");
            this.isDetail = false;
            this.user_type = localStorage.getItem("user_type");
            if (this.user_type == "store") {
              this.storeinfo();
            } else {
              this.userinfo();
            }
          }
          else{
                this.$message({
                    message: data.info,
                    type: "error"
                });
          }
        }
      });
    },
    query_fanli() {
      this.p++;

      let params = {
        pagesize: 5,
        p: this.p,
        token: localStorage.getItem("user_token")
      };

      let id = "";
      if (this.user_type == "store") {
        id = this.$route.query.store_id;
        params["user_type"] = "store";
        params["store_id"] = id;
      } else {
        id = this.$route.query.id;
        params["user_type"] = "user";
        params["uid"] = id;
      }

      http.get(this, {
        url: "ucenter/fanli_list",
        data: params,
        dataType: "json",
        success: function(data) {
          console.log(data);
          if (data.status == 1) {
            let info = data.info;
            if (this.p <= info.total_page) {
              this.comm_list = this.comm_list.concat(info.list);
            }
            console.log(this.comm_list);
          }
        }
      });
    },
    storeinfo() {
      http.get(this, {
        url: "store/store_info",
        data: {
          token: localStorage.getItem("user_token")
        },
        dataType: "json",
        success: function(data) {
          console.log("门店信息:", data);
          if (data.status == 1) {
            this.user_info = data.info;
            this.user_type = "store";
            this.user_info.user_fanli = this.user_info.store_fanli;
            // this.total_commission = data.info.total_commission;
            // this.store_count = data.info.store_count;
            localStorage.setItem("user_name", data.info.store_name);
            //localStorage.setItem("user_head", data.info.headimgurl);
            localStorage.setItem("user_type", "store");
            this.p = 0;
            this.comm_list = [];
            this.query_fanli();
          } else {
          }
        }
      });
    }
  },
  created() {
    
  },
  activated() {
    this.user_type = localStorage.getItem("user_type");
    if (this.user_type == "store") {
      this.storeinfo();
    } else {
      this.userinfo();
    }
  }
};
</script>

<style scoped lang="scss">
.qrcode-wrapper {
}

.top-wrap {
  $letHeight: 1.3rem;

  .title-txt {
    height: $letHeight;
    line-height: $letHeight;
    text-align: center;
    background-color: $c-white-light;

    span {
      color: $c-mainC;
      display: inline-block;
      line-height: $letHeight;
      vertical-align: middle;

      &.title-01 {
        font-size: 0.38rem;
      }
    }
  }
}

.main-wrap {
  @extend .cmm-border;
}

.img-wrap {
  width: 5rem;
  height: 5rem;
  margin: 0.4rem auto;
  overflow: hidden;
  position: relative;
  padding: 0.22rem;

  .ad-img {
    @extend .cmm-translateXY;
    width: 100%;
  }
}

.qrcode-box {
  text-align: center;
  width: 100%;
  .qrcode-txt {
    width: 5.15rem;
    border-radius: 0.8rem;
    background-color: $c-danger;
    font-size: 0.52rem;
    color: $c-white-light;
    $letHeight: 0.6rem;
    height: $letHeight;
    line-height: $letHeight;
    text-align: center;
    margin: 0.48rem auto;
    display: inline-block;
    letter-spacing: 0.1rem;
    font-weight: bold;
  }
}
.title-txt-new {
  height: 1rem;
  line-height: 1rem;
  text-align: center;
  background-color: $c-white-light;
  font-size: 0.32rem;
}
.picker {
  padding: 0 10px;
  border-top: 0.15rem solid #eeeeee;
  .el-date-editor.el-input,
  .el-date-editor.el-input__inner {
    width: 2.2rem;
  }
  /deep/.el-input__inner {
    border: none;
  }
}
.shop_table_header {
  display: flex;
  line-height: 1rem;
  padding: 0 0.2rem;
  border-bottom: 1px solid #eeeeee;
  & > div.th_one {
    flex: 2;
  }
  & > div.th_two {
    flex: 1;
  }
}
.shop_table_table {
  & > li {
    padding: 0.25rem 0.2rem;
    display: flex;
    & > div.td_one {
      flex: 2;
      & > div.td_item_one {
        margin-bottom: 0.2rem;
      }
      & > div.td_item_two {
        & > span {
          font-size: 0.08rem;
        }
      }
    }
    & > div.td_two {
      flex: 1;
      & > div.td_item_one {
        margin-bottom: 0.2rem;
      }
      & > div.td_item_two {
        & > span {
          font-size: 0.08rem;
        }
      }
    }
  }
}
.title_box {
  height: 0.9rem;
  line-height: 0.9rem;
  font-size: 0.36rem;
  text-align: center;
}
.commission {
  border-top: 0.15rem solid #eeeeee;
  font-size: 0.3rem;
  .com_box {
    height: 1rem;
    line-height: 1rem;
    padding: 0 0.3rem;
    border-bottom: 1px solid #eeeeee;
  }
}
.fl {
  float: left;
}
.fr {
  float: right;
}
.com_title {
  height: 1rem;
  line-height: 1rem;
  text-align: center;
  font-size: 0.36rem;
}
.com_title1 {
  height: 1rem;
  line-height: 1rem;
  font-size: 0.36rem;
}
.com_ul {
  li {
    padding: 0.25rem 0.3rem;
    border-bottom: 1px solid #eeeeee;
    .com_top {
      line-height: 0.5rem;
      margin-bottom: 0.2rem;
    }
    .com_name {
      float: left;
    }
    .com_orderId {
      font-size: 0.3rem !important;
      float: left;
    }
    .com_price {
      font-size: 0.5rem;
      color: #ff4c4c;
      float: right;
    }
    .com_time {
      float: right;
    }
  }
}
.com_money {
  font-size: 0.4rem;
  color: #ff4c4c;
}
.shop_close {
  position: absolute;
  right: 0.3rem;
  top: 0.32rem;
  font-size: 0.32rem;
}
.add_clerk {
  padding-top: 0.8rem;
  .title {
    font-size: 0.3rem;
    margin-bottom: 0.6rem;
    margin-left: 0.2rem;
  }
  /deep/.el-input__inner {
    border-top: none;
    border-left: none;
    border-right: none;
  }
}
.shop_detail {
  position: relative;
  .shop_name {
    height: 1rem;
    line-height: 1rem;
    padding: 0 0.4rem;

    font-size: 0.3rem;
  }
  .shop_other_box {
    border-bottom: 0.013333rem solid #eeeeee;
    border-top: 0.013333rem solid #eeeeee;
  }
  .shop_other {
    height: 0.8rem;
    line-height: 0.8rem;
    padding: 0 0.4rem;
  }
  .shop_price {
    height: 4rem;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 0.32rem;
    color: #c09a5b;
  }
}
/deep/.el-form-item {
  margin-bottom: 0.4rem;
}
/deep/.el-form-item__label {
  text-align: center;
}
.submit-box {
  padding: 0 0.2rem;
}
</style>
